package com.xiao.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.fastjson.JSON;
import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.category.AddArticleCategoryDto;
import com.xiao.domain.dto.category.ArticleCategoryListDto;
import com.xiao.domain.dto.category.SaveUpdateCategoryDto;
import com.xiao.domain.pojo.ArticleCategory;
import com.xiao.domain.vo.category.ExcelCategoryVo;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.mapper.ArticleCategoryMapper;
import com.xiao.service.ArticleCategoryService;
import com.xiao.utils.BeanCopyUtils;
import com.xiao.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
@Api(tags = "文章分类")
public class ArticleCategoryController {

    @Autowired
    ArticleCategoryService articleCategoryService;

    @GetMapping("/listAllCategory")
    @ApiOperation("获取所有文章分类列表")
    public ResponseResult<ArticleCategory> listAllCategory(){
        ResponseResult categoryList = articleCategoryService.getCategoryList();
        return categoryList;
    }

    @GetMapping("/list")
    @ApiOperation("获取文章分类列表，支持模糊查询与分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小"),
            @ApiImplicitParam(name = "dto",value = "接收查询的参数-dto")
    })
    public ResponseResult categoryList(Integer pageNum, Integer pageSize, ArticleCategoryListDto dto){// GET 不需要用 @RequestBody
        ResponseResult responseResult = articleCategoryService.categoryList(pageNum, pageSize, dto);
        return responseResult;
    }

    @PostMapping
    @ApiOperation("依据dto,新增文章分类")
    @ApiImplicitParam(name = "dto",value = "新增文章分类-dto")
    public ResponseResult addArticleCategory(@RequestBody AddArticleCategoryDto dto){
        ResponseResult responseResult = articleCategoryService.addArticleCategory(dto);
        return  responseResult;
    }

    @GetMapping("/{id}")
    @ApiOperation("依据分类ID,返回分类信息")
    @ApiImplicitParam(name = "id",value = "分类id")
    public ResponseResult getCategoryById(@PathVariable("id") Long id){
        ResponseResult responseResult = articleCategoryService.getCategoryById(id);
        return  responseResult;
    }

    @PutMapping
    @ApiOperation("依据 dto,保存修改")
    @ApiImplicitParam(name = "dto",value = "修改内容-dto")
    public ResponseResult saveUpdateByDto(@RequestBody SaveUpdateCategoryDto dto){// @RequestBody 不能忘记！！！
        ResponseResult responseResult = articleCategoryService.saveUpdateByDto(dto);
        return responseResult;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据分类id列表,删除分类。")
    @ApiImplicitParam(name = "idList",value = "分类id列表")
    public ResponseResult delCategoryByIdList(@PathVariable("id") List<Long> idLis){
        ResponseResult responseResult = articleCategoryService.delCategoryByIdList(idLis);
        return responseResult;
    }

    @Autowired
    ArticleCategoryMapper articleCategoryMapper;

    @GetMapping("/export")
    @ApiOperation("导出所有分类数据,和搜索结果无关")
    @ApiImplicitParam(name = "response",value = "响应体")
    @CrossOrigin// 支持跨源请求
    public void export(HttpServletResponse response){
        try {
            // 1. 设置下载请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            // 2. 获取需要导出的数据
            List<ArticleCategory> list = articleCategoryMapper.getAllCategory();// 获取所有数据，包含逻辑删除的

            List<ExcelCategoryVo> vos = BeanCopyUtils.copyBeanList(list, ExcelCategoryVo.class);
            // 3. 把数据写入 Excel
            ExcelWriterBuilder write = EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class);
            write.autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(vos);

        } catch (Exception e) {
            // 如果失败了 响应 json
            ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(responseResult));
        }

    }

}
